//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================



using System;
using System.Collections.Generic;
using System.Text;
using Shp.Runtime.Contract;
using System.Data;
using LitwareHR.Portfolio.Gateways;
using System.Globalization;
using LitwareHR.PublicApp.Presenters.Properties;
using LitwareHR.Recruiting.Contract;
using System.Collections.ObjectModel;


namespace LitwareHR.PublicApp.Presenters.Careers.Evaluation
{
    public class RecruiterPresenter
    {
        private IRecruiterView _view;

        public RecruiterPresenter(IRecruiterView view)
        {
            _view = view;
        }

        public void OnViewLoaded()
        {
            StringPair[][] workflowList = RecruitingGateway.GetRecruitingWorkflowList(true);
            Collection<Position> positions = RecruitingGateway.GetOpenPositionEntityList();
            DataTable table = new DataTable();
            table.Locale = CultureInfo.CurrentCulture;

            TranslateListToDataTable(workflowList, positions, table);

            _view.ShowWorkflows(table);
        }

        private static string GetPositionName(Collection<Position> positions, Guid id)
        {
            string name = string.Empty;
            
            for (int i = 0; (i < positions.Count) && (String.IsNullOrEmpty(name)); i++)
            {
                if (positions[i].Id.Equals(id))
                {
                    string title = string.Empty;
                    string code = string.Empty;
                    for (int f = 0; (f < positions[i].Fields.Count); f++)
                    {
                        if (positions[i].Fields[f].Name.ToUpper(CultureInfo.InvariantCulture).Equals("TITLE"))
                        {
                            title = positions[i].Fields[f].Value;
                        }
                        if (positions[i].Fields[f].Name.ToUpper(CultureInfo.InvariantCulture).Equals("CODE"))
                        {
                            code = positions[i].Fields[f].Value;
                        }
                    }
                    name = string.Format(CultureInfo.CurrentCulture, "{0} ({1})", title, code);
                    
                }
            }
            return name;
        }

        private static void TranslateListToDataTable(StringPair[][] workflowList, Collection<Position> positions, DataTable table)
        {
            if (workflowList.Length > 0)
            {
                table.Columns.Add("NextStepUrl");
                foreach (StringPair field in workflowList[0])
                {
                    if (!String.Equals(field.Key, "WorkflowId") 
                        || !String.Equals(field.Key, "PositionId")
                        || !String.Equals(field.Key, "ResumeId"))
                    {
                        table.Columns.Add(field.Key);
                    }
                }

                table.Columns.Add("Position");
                table.Columns.Add("ResumeDetailUrl");

                foreach (StringPair[] record in workflowList)
                {
                    DataRow row = table.NewRow();
                    List<StringPair> workflow = new List<StringPair>(record);
                    StringPair NextStep = StringPair.Get("NextStep", workflow);
                    StringPair WorkflowId = StringPair.Get("WorkflowId", workflow);
                    StringPair PositionId = StringPair.Get("PositionId", workflow);
                    StringPair ResumeId = StringPair.Get("ResumeId", workflow);
                    row["NextStepUrl"] = string.Format(CultureInfo.CurrentCulture, Resources.EvaluationUrlFormat, NextStep.Value, WorkflowId.Value);
                    string positionName = GetPositionName(positions, new Guid(PositionId.Value));
                    row["Position"] = positionName;
                    row["ResumeDetailUrl"] = string.Format(CultureInfo.CurrentCulture, Resources.ViewResumeDetailsUrlFormat, ResumeId.Value.ToString(), positionName);
                    
                    
                    foreach (StringPair field in workflow)
                    {
                        if (!string.Equals(field.Key, "WorkflowId"))
                        {
                            row[field.Key] = field.Value;
                        }
                    }
                    
                    table.Rows.Add(row);
                }
            }
        }
    }
}
